import configparser
from configurations.files_path_info import file_paths
from handlers.handler_log import logger


class HandlerIni(object):

    def __init__(self, file_path=file_paths['config.ini']):
        try:
            self.config = configparser.ConfigParser()
            self.config.read(file_path)
        except Exception as e:
            logger.exception(f'IniHandler初始化异常：{e}')

    def get_value(self, section, option) -> str:
        """
        获取配置文件的值
        :param section: 头参数
        :param option: 下级参数key的value值
        :return:
        """
        try:
            return self.config.get(section, option)
        except Exception as e:
            logger.exception(f'IniHandler获取配置文件的值异常：{e}')

    def get_host_value(self, option):
        """
        获取section为Host的option值
        :param option: option值
        :return: option的value
        """
        try:
            return self.get_value('Host', option)
        except Exception as e:
            logger.exception(f'获取section为Host的option值异常：{e}')

    def get_interface_value(self, option):
        """
        获取section为Interface的option值
        :param option: option值
        :return: option的value
        """
        try:
            return self.get_value('Interface', option)
        except Exception as e:
            logger.exception(f'获取section为Interface的option值异常：{e}')

    def get_mysql_value(self, option):
        """
        获取section为MySQL的option值
        :param option: option值
        :return: option的value
        """
        try:
            return self.get_value('MySQL', option)
        except Exception as e:
            logger.exception(f'获取section为MySQL的option值异常：{e}')

